//
//  XLMainViewController.swift
//  SinaProjectTest
//
//  Created by zhanghaifeng on 16/10/22.
//  Copyright © 2016年 BBSWaitting. All rights reserved.
//

import UIKit
import SVProgressHUD

class XLMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        // 创建自定义Btn
        let tabBar = XLTabBar()
        // 02 闭包的实例化
        tabBar.closure = {
            // 04 接收回调
            if !XLUserAccountViewModel.sharedTools.isLogin{
                SVProgressHUD.showError(withStatus: "请先登录, 亲~")
                return
            }
            // 代码执行到此代表登录
            // 获取window 获取最上面的window
            let window = UIApplication.shared.windows.last!
            let composeView = XLComposeView()
            window.addSubview(composeView)
            
        }
        // 通过KVC 给计算型属性赋值
        setValue(tabBar, forKey: "tabBar")
        // 首页控制器
        addChildViewController(vc: XLHomeViewController(), imageNamed: "tabbar_home", title: "首页")
        // 消息控制器
        addChildViewController(vc: XLMessageViewController(), imageNamed: "tabbar_message_center", title: "消息")
        // 发现控制器
        addChildViewController(vc: XLDiscoverViewController(), imageNamed: "tabbar_discover", title: "发现")
        // 我控制器
        addChildViewController(vc: XLProfileViewController(), imageNamed: "tabbar_profile", title: "我")
        
    }
    
    // 抽取方法添加四个字控制器
    func addChildViewController(vc: UIViewController, imageNamed: String, title: String){
        // 设置title
        vc.title = title
        // 设置字体颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: UIControlState.selected)
        // 设置图片
        vc.tabBarItem.image = UIImage(named: imageNamed)
        // 设置高亮图片 并修改选中颜色为原生渲染方式
        vc.tabBarItem.selectedImage = UIImage(named: "\(imageNamed)_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        // 设置根控制器
        let Nav = XLNavigationController(rootViewController: vc)
        // 添加导航控制器
        addChildViewController(Nav)
    }
    
    

}
